# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  You should have received a copy of the GNU General Public License
#
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# Authors: Gregory Tardivel

bl_info = {
    'name': 'Export: JSON (.json)',
    'author': 'Gregory Tardivel',
    'version': (1,0,0),
    "blender": (2,69,0),
    'location': "File > Export > json (.json)",
    'description': 'Export files in json format (.json)',
    'warning': '',
    'category': 'GR3G'}

import bpy
import os
import time
import math
import mathutils
from mathutils import Vector, Matrix, Quaternion
from bpy.props import *

class ExportJSON(bpy.types.Operator):
    bl_idname = 'export_scene.json'
    bl_description = 'Export from JSON file format (.json)'
    bl_label = 'Export JSON'
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_options = {'UNDO'}
    
    def execute(self, context):
        file = open("export.json", "w", encoding="utf8", newline="\n")
        fw = file.write
        objets = []
        fw('{\n')
        for item in bpy.data.objects:
            if (item.type == 'MESH'):
                objets.append(item)
        fw("\tm: [\n")
        for item in objets:
            fw('\t\t{\n')
            fw('\t\t\tname: %s,\n' % (item.name))
            fw('\t\t\tfaces: [\n')
            for face in item.data.polygons:
                verts_in_face = face.vertices[:]
                fw("\t\t\t\t{\n")
                fw("\t\t\t\t\ti: %d,\n" % (face.index))
                fw("\t\t\t\t\tn: {x: %.6f, y: %.6f, z: %.6f},\n" % (face.normal.x, face.normal.y, face.normal.z))
                fw("\t\t\t\t\tv: [\n")
                for vert in verts_in_face:
                    fw("\t\t\t\t\t\t{x: %.6f, y: %.6f, z: %.6f}" % (item.data.vertices[vert].co[:]))
                    if (vert == verts_in_face[-1]):
                        fw("\n")
                    else:
                        fw(",\n")
                fw("\t\t\t\t\t]\n")
                fw("\t\t\t\t}")
                if (face == item.data.polygons[-1]):
                    fw("\n")
                else:
                    fw(",\n")
            fw('\t\t\t]\n')
            if (item == objets[-1]):
                fw('\t\t}\n')
            else:
                fw('\t\t},\n')
        fw("\t]\n")
        fw('}')
        file.close()
        return {'FINISHED'}

def menu_func_export(self, context):
    self.layout.operator(ExportJSON.bl_idname)

def register():
    bpy.utils.register_module(__name__)
    bpy.types.INFO_MT_file_import.append(menu_func_export)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    unregister()
    register()